package com.demo14_最长公共前缀;

public class Solution {
    public static void main(String[] args) {
        String[] strings={"flower","flower","flower","flower"};
        String s = longestCommonPrefix(strings);
        System.out.println(s);
    }
    public static String longestCommonPrefix(String[] strs) {
        if (strs.length==0){
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        int minLength=strs[0].length();
        //1.求字符数组的最小的长度
        for (int i = 1; i < strs.length; i++) {
            minLength=Math.min(minLength,strs[i].length());
        }
        //2.遍历
        boolean flag=true;
        for (int i = 0; i < minLength; i++) {
            for (int j = 0; j < strs.length; j++) {
                // 固定和第一个比就可以了
                if (!(strs[j].charAt(i)==strs[0].charAt(i))){
                    flag=false;
                    break;

                }
            }
            if (!flag){
                break;
            }
            stringBuilder.append(strs[0].charAt(i));
        }

        return stringBuilder.toString();
    }
}
